const express = require('express')
const app = express()
var bodyParser = require('body-parser');
var multer = require('multer'); // v1.0.5
var upload = multer({dest: 'upload'}); // for parsing multipart/form-data
var userDao = require('./dao/userDAO');
app.use((req, res, next) => {
    // 设置是否运行客户端设置 withCredentials
    // 即在不同域名下发出的请求也可以携带 cookie
    res.header("Access-Control-Allow-Credentials",true)
    // 第二个参数表示允许跨域的域名，* 代表所有域名  
    res.header('Access-Control-Allow-Origin', '*')
    res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, OPTIONS') // 允许的 http 请求的方法
    // 允许前台获得的除 Cache-Control、Content-Language、Content-Type、Expires、Last-Modified、Pragma 这几张基本响应头之外的响应头
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With')
    if (req.method == 'OPTIONS') {
        res.sendStatus(200)
    } else {
        next()
    }
})
app.use(express.static('public'))
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.post('/upload', upload.fields([{name:"image"},{name:"image2"}]), function (req, res, next) {
    console.log(req.body);
    console.log(req.file);
    console.log(req.files);
    res.send("success");
});
app.post('/login',function(req,res,next){
    userDao.queryTokenByUsernameAndPassword(req,res,next);
})
app.post('/queryMenuByUserId',function(req,res,next){
    userDao.queryMenuByUserId(req,res,next);
})
app.post('/list',function(req,res,next){
    res.send({
        state:"200",
        //state:""//fail,
        //msg:"",
        data:[{
            id:"0",
            name:"小明",
            age:10
        },{
            id:"1",
            name:"小强",
            age:11
        }]
    })
})
app.listen(3000, () => console.log('Example app listening on port 3000!'))